#timers
import os, sys, datetime

class Timer:
    def __init__(self):
        self.timeout = 0
        self.t0 = datetime.datetime.now()#.replace(microsecond = 0)
        self.run = False
        self.cancelled = False

    def setTimeoutS(self, t):
        if t > 0:
            self.timeout = t
            self.t0 = datetime.datetime.now()#.replace(microsecond = 0)
            self.run = True
            self.cancelled = False

    def setTimeoutM(self, t):
        self.setTimeoutS(t * 60)

    def expired(self):
        if self.run:    # self.run used to avoid calculating time diff each call once expired
            now = datetime.datetime.now()#.replace(microsecond=0)
            if  now - self.t0 >= datetime.timedelta(seconds = self.timeout):
                print now
                print self.t0
                print datetime.timedelta(seconds = self.timeout)
                self.run = False
                return True
            else:
                return False
        else:
            return not self.cancelled

    def cancel(self):
        self.cancelled = True
        self.run = False


class Timers:
    def __init__(self):
        self.timer = {}

    def addTimer(self, name):
        if not name in self.timer:
            self.timer[name] = Timer()

    def deleteTimer(self, name):
        if name in self.timer:
            del self.timer[name]

    def setTimeoutS(self, name, t):
        if name in self.timer:
            self.timer[name].setTimeoutS(t)

    def setTimeoutM(self, name, t):
        if name in self.timer:
            self.timer[name].setTimeoutM(t)

    def expired(self, name):
        if name in self.timer:
            return self.timer[name].expired()
        return True

